package fr.paris8.iut.info.iddl;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */




import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Set;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.owllink.OWLlinkHTTPXMLReasonerFactory;
import org.semanticweb.owlapi.owllink.OWLlinkReasoner;
import org.semanticweb.owlapi.owllink.OWLlinkReasonerConfiguration;
import org.semanticweb.owlapi.owllink.builtin.requests.CreateKB;
import org.semanticweb.owlapi.owllink.builtin.requests.ReleaseKB;
import org.semanticweb.owlapi.owllink.builtin.requests.Tell;
import org.semanticweb.owlapi.owllink.builtin.response.KB;
import org.semanticweb.owlapi.owllink.builtin.response.OK;



/**
 *
 * @author MYAMBITION
 */
public class Connector {

    private static OWLOntologyManager manager;

    /**
     * @return the url
     */
    public URL getUrl() {
        return url;
    }
    private OWLlinkHTTPXMLReasonerFactory rFactory;
    private OWLlinkReasoner  reasoner;
    private OWLlinkReasonerConfiguration reasonerConfiguration;
    private Set<OWLAxiom> axioms;
    private URL url = null;
 
    

    public Connector(String urlReasoner,int port)
    {        
        manager = OWLManager.createOWLOntologyManager();
       try {
            url = new URL(urlReasoner+":"+port);
       } catch (MalformedURLException e) {
            e.printStackTrace();
       }
       reasonerConfiguration = new OWLlinkReasonerConfiguration(url);
       rFactory = new OWLlinkHTTPXMLReasonerFactory();        
        
    }

    public boolean isConsistent(String ontoUri, java.util.Set<OWLAxiom> axioms) throws OWLOntologyCreationException {
	boolean response  =  false;

        OWLOntology onto  = manager.createOntology(IRI.create(ontoUri));
        reasoner = rFactory.createReasoner(onto,reasonerConfiguration);
        //Create a new (empty) knowledge base ...
        //CreateKB createKBRequest = new CreateKB();
        //KB kbResponse = reasoner.answer(createKBRequest);
        //... and transfer a set of axioms (e.g., from another ontology, an temporary ontology etc.) to it
        Tell tellRequest = new Tell(IRI.create(ontoUri),axioms);
        OK okResponse = reasoner.answer(tellRequest);

        response = reasoner.isConsistent();


        //Don't forget to release the knowledge base before terminating the application
        ReleaseKB releaseKBRequest = new ReleaseKB(IRI.create(ontoUri));
        okResponse = reasoner.answer(releaseKBRequest);

//	OWLOntology onto1 = null;
//        try {
//                onto1 = manager.createOntology( IRI.create(ontoUri) );
//        }
//        catch (OWLOntologyCreationException ex) { ex.printStackTrace(); }
//
//        //OWLOntologyImpl onto1 = new OWLOntologyImpl( manager, new OWLOntologyID( IRI.create( "file:/tmp/MyOnt.owl" ) ) );
//        //JE2010: OWLOntologyImpl onto1 = new OWLOntologyImpl( facto, new OWLOntologyID( IRI.create(ontoUri) ) );
//
//
//
//        //System.out.println( "Number of axioms of the initial onto :"+ axiomSet.size() );
//
//        //System.out.println( "Number of added axioms : "+ axioms.size() );
//
//        //OWLOntologyChangeVisitorAdapter xc = new OWLOntologyChangeVisitorAdapter();
//        Iterator iter= axioms.iterator () ;
//        while (  iter.hasNext (  )   )  {
//                OWLAxiom ax = (OWLAxiom)iter.next ();
//                //System.out.println("Axiom= "+ ax.toString());
//                //AddAxiom addaxiom = new AddAxiom(onto, ax);
//                AddAxiom addaxiom = new AddAxiom(onto1, ax);
//                //addaxiom.accept(xc);
//                //onto1.applyChange( addaxiom );
//                manager.applyChange(addaxiom);
//        }
//        //reasoner.loadOntology( onto1 );//JE??
//        reasoner =rFactory.createReasoner(onto1);
//
//        //reasoner.getKB().realize();
//        //ReasonerManager.reasoner.getKB().printClassTree();
//
//        response = reasoner.isConsistent();
//        //JE2010: reasoner.unloadOntology( onto1 );
//
        return response;
	}

    //Fonction de verifier un ontology est consistent ou pas
    public boolean isConsistent(OWLOntology ontology)//String ontoUri, java.util.Set<OWLAxiom> axioms, LocalNode node)
     {    
//        try
//        {
               //OWLOntology ontology = manager.createOntology(IRI.create(urlOnto));
               //OWLOntology ontology = manager.createOntology(IRI.create("TuanAnh"));
               reasoner =rFactory.createReasoner(ontology);

                //reasoner.setSemantics(Semantics.DL);
              if (reasoner.isConsistent()) {
                return true;
              } else
                return false;
//        }catch(Exception e)
//        {
//            return false;
//        }
        
     }

   

    public void Request() throws MalformedURLException, OWLOntologyCreationException
    {            
        OWLOntology onto  = manager.createOntology(IRI.create("http://alignapi.gforge.inria.fr/tutorial/myOnto.owl"));
        reasoner = rFactory.createReasoner(onto,reasonerConfiguration);
        //Create a new (empty) knowledge base ...
        CreateKB createKBRequest = new CreateKB();
        KB kbResponse = reasoner.answer(createKBRequest);
        //... and transfer a set of axioms (e.g., from another ontology, an temporary ontology etc.) to it
        Tell tellRequest = new Tell(kbResponse.getKB(),axioms);
        OK okResponse = reasoner.answer(tellRequest);

        //Don't forget to release the knowledge base before terminating the application
        ReleaseKB releaseKBRequest = new ReleaseKB(kbResponse.getKB());
        okResponse = reasoner.answer(releaseKBRequest);
    }


    public  String GetName()
    {
        try
        {
            OWLOntology onto  = manager.createOntology(IRI.create("http://127.0.0.1:8082"));
            reasoner = rFactory.createReasoner(onto,reasonerConfiguration);

        }
        catch(Exception e)
        {

        }
        return reasoner.getReasonerName();
    }
}

